<?php
require('lib/verify.php');
require('lib/has_char.php');
require('lib/is_approved.php');
require('lib/is_admin.php');

$_category = "warmath";
$_title = "Admin / Schedule a Raid";

$raid_system_id = $_POST["system_id"];
$raid_instance = $_POST["instance_id"];
$month = $_POST["month"]+1;
$day = $_POST["day"];
$year = $_POST["year"];

$system_id = $system["system_id"];


function verify_names($names, $db, $system_id) {
  $nlook = array();
$exam = explode("\n", $names);
while(list($n, $name) = each($exam)) {
  $name = ucfirst(strtolower(trim($name)));
  $sql = "select * from wm_characters where character_system_id = $system_id and character_name='$name' and character_approved=1";
  $result = $db->query($sql);
  if($db->num_rows($result)!=1) {
    $err = "Unrecognized character '<b>$name</b>'.  Please make sure he or she is registered and approved.";
    header("Location: error.php?error=$err");
    exit;
  }
  $chara = $db->fetch_assoc($result);
  $nlook[$name] = $chara["character_id"];
}
 return $nlook;

}

$attendance = $_POST["attendance"];

$name_lookup = verify_names($attendance, $db, $system_id);


$hour = $_POST["hour"];
$minute = $_POST["minute"];
$ampm = $_POST["ampm"];
if($minute<10) {
  $minute = "0" . $minute;
}

$date_string = "$month/$day/$year $hour:$minute:00 $ampm";

$comments = $db->escape($_POST["comments"]);

$raid_time = strtotime($date_string);

$sql = "insert into wm_scheduled_raids (scheduled_raid_instance, scheduled_raid_datetime, scheduled_raid_system_id, scheduled_raid_comments) values ('$raid_instance', FROM_UNIXTIME('$raid_time'), '$raid_system_id', '$comments')";

$result = $db->query($sql);
$ii = $db->insert_id($result);

while(list($bid, $on) = each($_POST["boss"])) {
  $sql = "insert into wm_scheduled_raid_bosses (scheduled_raid, scheduled_raid_boss, scheduled_raid_system_id) values ('$ii', '$bid', '$raid_system_id')";
  $result = $db->query($sql);
}
/*
$now = time();

$chara = $character["character_id"];

$log_entry = "Created raid sign up $ii; $raid_time in instance: $raid_instance";

$sql = "insert into wm_logs (log_character, log_type, log_description, log_datetime, log_system_id, log_admin) values ($chara, 'Signup', '$log_entry', FROM_UNIXTIME($now), $raid_system_id, '1')";
$result = $db->query($sql);
*/

//do attendance
while(list($name, $cid) = each($name_lookup)) {
  $sql = "insert into wm_scheduled_signups (scheduled_signup_character, scheduled_signup_raid, scheduled_signup_system_id) values ('$cid', '$ii', '$system_id')";
  $res = $db->query($sql);
}


header("Location: signup.php?id=$ii");
?>

